package com.blb.day20231107login.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blb.day20231107login.entity.Permission;
import com.blb.day20231107login.entity.Role;
import com.blb.day20231107login.entity.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author chenheng
 * @since 2023-11-06
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 按用户名查询所有角色
     * @param username
     * @return
     */
    @Select("select r.* from user u join user_role ur on u.id = ur.user_id\n" +
            "join role r on r.id = ur.role_id\n" +
            "where u.username = #{username}")
    List<Role> selectRolesByUsername(String username);

    /**
     * 按用户名查询所有权限
     * @param username
     * @return
     */
    @Select("select p.* from user u \n" +
            "join user_role ur on u.id = ur.user_id\n" +
            "join role r on r.id = ur.role_id\n" +
            "join role_permission rp on r.id = rp.role_id\n" +
            "join permission p on p.id = rp.permission_id\n" +
            "where u.username = #{username}")
    List<Permission> selectPermissionsByUsername(String username);

    /**
     * 按用户名查询权限和子权限
     */
    List<Permission> selectPermissionsWithChildrenByUsername(String username);
}
